package com.hc.baselibray.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.util.HashMap;

public class AlterController {
    private  AlertDialog mDialog;
    private  Window mWindow;

    public AlertDialog getmDialog() {
        return mDialog;
    }

    public Window getmWindow() {
        return mWindow;
    }

    public AlterController(AlertDialog mDialog, Window window) {
        this.mDialog = mDialog;
        this.mWindow=window;
    }

    //存储数据
    public static class AlertParams {
        public Context mContext;
        public int mThemeResId;
        //点击空白能否取消
        public boolean mCancelable=true;
        //cancel
        public DialogInterface.OnCancelListener mOnCancerListener;
        //dimiss
        public DialogInterface.OnDismissListener mOnDismissListener;
        //key
        public DialogInterface.OnKeyListener mOnKeyListener;
        public View mView;
        public int mViewLayoutResId;
        //设置文本，存放字体修改（可能存在多个文字修改）
       // HashMap<Integer,CharSequence> textMap=new HashMap<>();
        //如果key为intger，value为object  SparseArray比hashmap更加高效
       public SparseArray<CharSequence> mTextMap=new SparseArray<>();
    ///设置点击
         public SparseArray<View.OnClickListener> mClickArray=new SparseArray<>();
         //宽度默认内容自适应
        public int mWidth= ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mHeight=ViewGroup.LayoutParams.WRAP_CONTENT;
        //入场和出场的动画资源
        public int mAnimations;
        //布局，默认剧中
        public int mGravity= Gravity.CENTER;


        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }
        //绑定和设置参数，某种程度等于init
        public void apply(AlterController mAltert) throws IllegalAccessException {
            //设置参数
            //1 设置布局
            DialogViewHelper viewHelper=null;
            if(mViewLayoutResId!=0){
                viewHelper=new DialogViewHelper(mContext,mViewLayoutResId);
            }
            if(mView!=null){
                viewHelper=new DialogViewHelper();
                viewHelper.setContentView(mView);
            }
            if(viewHelper==null){
                    throw  new IllegalAccessException("请设置布局文件");
            }
            //给dialog设置布局,,这里好绕
            mAltert.getmDialog().setContentView(viewHelper.getmContentView());
            //2。设置文本
            int textArraySize=mTextMap.size();
            for(int i=0;i<textArraySize;i++){
                viewHelper.setText(mTextMap.keyAt(i),mTextMap.valueAt(i));
            }
            //3设置点击
            int clickArraySize=mClickArray.size();
            for(int i=0;i<clickArraySize;i++){
                viewHelper.setOnClickListener(mClickArray.keyAt(i),mClickArray.valueAt(i));
            }
            //4 设置自定义的效果， 全屏从底部弹窗，默认动画
            //window知识点！！！！！！！！和dialog有啥关系？？？？
            Window window=mAltert.getmWindow();
            //设置位置
            window.setGravity(mGravity);
            //设置动画
            if(mAnimations!=0){
                window.setWindowAnimations(mAnimations);
            }
            //设置狂高

            WindowManager.LayoutParams params=window.getAttributes();
            params.width=mWidth;
            params.height=mHeight;
            window.setAttributes(params);
        }
    }
}
